/**
 * 
 */
package jvs.sh;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import jvs.peer.sec.User;

/**
 * @author qiangli
 * 
 */
public class CommandConsole {
	public class MyConsole implements Console {
		protected String logTag = "log: ";
		protected String outTag = "--- ";
		protected String okTag = "200 ";
		protected String errTag = "500 ";
		
		public MyConsole() {
		}
		
		public void log(String s) {

		}

		public void err(String s) {
			print(errTag + s);
		}

		private void print(String s) {
			try {
				out.write(s.getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		public void ok(String s) {
			print(okTag + s );
		}

		public void out(String s) {
			print(outTag + s);
		}

		public void flush() {
			try {
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		public OutputStream data() {
			return out;
		}

		public void input(String s) {
			ci.parse(s);
		}

		public void status(String code, String s) {
			print(code+" "+s);
		}
	}

	public class MyShell implements IShell {
		public void exit() {
			bye();
		}

		public long getLastAccess() {
			// TODO Auto-generated method stub
			return 0;
		}

		public void connect(User user) {
			// TODO Auto-generated method stub
			
		}

		public boolean isConnected() {
			// TODO Auto-generated method stub
			return false;
		}

		public String getEnv(String string) {
			// TODO Auto-generated method stub
			return null;
		}

		public void setLastAccess(long currentTimeMillis) {
			// TODO Auto-generated method stub
			
		}

		public boolean ping(String peer) {
			// TODO Auto-generated method stub
			return false;
		}

		public String id() {
			// TODO Auto-generated method stub
			return null;
		}
	}
	
	private OutputStream out = System.out;
	private CommandInterpreter ci;
	private IShell sh;
	private Console con;

	protected void exit(int i) {
		//System.exit(0);
	}

	protected void bye() {
		//System.exit(0);
	}

	public CommandConsole() {
		
		init();
	}

	public CommandConsole(InputStream in, OutputStream out) {
		init();
	}
	
	private void init() {
		con = new MyConsole();
		ci = new CommandInterpreter(null);
		sh = new MyShell();
		ci.setShell(sh);
		ci.setConsole(con);
		//
		sh.connect(new User("root", "root"));
	}

	public void setOut(OutputStream out) {
		this.out = out;
	}

	public void input(String s) {
		con.input(s);
	}
	
	public void input(InputStream in) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		try {
			String line = null;
			StringBuffer sb = new StringBuffer();
			boolean multiline = false;
			//
			while ((line = br.readLine()) != null) {
				int len = line.length();
				if (len == 0) {
					continue;
				}
				if (line.charAt(len - 1) == '\\') {
					sb.append(line.substring(0, len - 1));
					sb.append("\r\n");
					multiline = true;
				} else {
					if (multiline) {
						sb.append(line);
						con.input(sb.toString());
						multiline = false;
						sb.setLength(0);
					} else {
						con.input(line);
					}
				}
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
